package org.lex.pci.ch02.similarity;

import java.util.List;

import org.lex.pci.ch02.data.Preferences;
import org.lex.pci.ch02.data.Preferences.Rate;

public class EuclideanDistanceSimilarityAlgorithm implements SimilarityAlgorithm {

	@Override
	public String getName() {
		return "Euclidean Distance Similarity";
	}

	@Override
	public double getSimilarity(Preferences prefs, String person1, String person2) {
		List<Rate[]> commons = prefs.getSharedRate(person1, person2);
		double result = 0.0f;
		if (null != commons && false == commons.isEmpty()) {
			double sum = 0.0;
			for (Rate[] pair : commons) {
				double rate1 = pair[0].getRate();
				double rate2 = pair[1].getRate();
				sum += Math.pow(rate1 - rate2, 2);
			}
			result = 1.0 / (1 + Math.sqrt(sum));
		}
		return result;
	}
}
